define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/stats/models'),
        utils = require('js/utils/tools'),
        app = Backbone.history;
    require('datetimepicker');

    var PayListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click #search': 'doSearch',
            'click #exportData': 'exportData',
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.PayCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        exportData: function (e) {
            e.preventDefault();
            if (this.collection.total == 0) {
                return;
            }
            if (this.collection.total > 1000) {
                notify.warning('警告', '数据量较大，请耐心等待');
            }
            utils.exportData(this.collection.url, this.options);
            return false;
        },
        doSearch: function () {
            var options = {},
                searched = {};
            searched.id = $('#searchId').val();
            searched.user_id = parseInt($("#searchUserId").val());
            searched.created_at_lower = $("#searchCreatedAtLower").val();
            searched.created_at_upper = $('#searchCreatedAtUpper').val();
            searched.type = parseInt($('#searchType').val());
            searched.pay_type = parseInt($('#searchPayType').val());
            searched.user_type = parseInt($('#searchUserType').val());
            searched.status = parseInt($('#searchStatus').val());

            if (!isNaN(searched.user_id)) {
                options.user_id = searched.user_id;
            }
            if (searched.id) {
                options.id = searched.id;
            }
            if (searched.created_at_lower || searched.created_at_upper) {
                options.created_at = {};
                if (searched.created_at_lower) options.created_at.$gte = searched.created_at_lower;
                if (searched.created_at_upper) options.created_at.$lt = searched.created_at_upper;
            }
            if (searched.type != -1) {
                options.activity_id = searched.type ? '$!null' : '$null';
            }
            if (searched.pay_type != -1) {
                options.pay_type = searched.pay_type;
            }
            if (searched.user_type != -1) {
                options.user_type = searched.user_type;
            }
            options.status = searched.status;
            options.is_fresh = 0;
            utils.saveSearched('stats/pay', searched);
            app.navigate(utils.composeQueryString('#stats/pay/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('stats/pay'),
                overview: this.collection.overview,
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || '-created_at',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#stats/pay/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('[data-type=date]').datetimepicker({
                defaultDate: false,
                format: 'YYYY-MM-DD'
            });


             $('.modify-item').magnificPopup({
                items: {
                    src: '#editWithdrawPanel',
                    type: 'inline'
                },
                midClick: true,
                closeOnBgClick: false,
                callbacks: {
                    beforeOpen: function () {
                        var data_id = $(this.st.el).closest('tr').data('id'),
                            choosen = that.collection.get(data_id);
                        $('#order_id').text(choosen.get('id'));
                        if(2 == choosen.get('status')){
                           $('#pay_status').text('已支付');
                        }else{
                            $('#pay_status').text('未支付');
                        }
                        $('#created_at').text(choosen.get('created_at'));
                        $('#updated_at').text(choosen.get('updated_at'));
                        $('#user_id').text(choosen.get('user_id'));
                        $('#user_type').text(USER_TYPE[choosen.get('user_type')]);
                        $('#recharge_type').text(PAY_TYPE[choosen.get('pay_type')]);
                        $('#recharge_price').text(choosen.get('price'));
                        $('#recharge_ent').text(choosen.get('activity_name'));

                        $('#submitClose').off('click').click(function() {
                            $.magnificPopup.close();
                        });

                    },
                }
            });

            utils.getPaginator(that.options, this.collection.total, '#stats/pay/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "stats/PayList.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            if(this.options.is_fresh === undefined) this.options.is_fresh = 1;
            var view = new PayListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});
